package com.example.demofdfs.test2;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

import org.junit.Test;

public class SynchronousQueueTest {
	
	public static void main(String[] args) throws InterruptedException {
//		new SynchronousQueueTest().test1();
//		new SynchronousQueueTest().test2();
		new SynchronousQueueTest().test3();
	}

//	@Test
	public void test1() throws InterruptedException {
		BlockingQueue<String> bq = new SynchronousQueue<>(true);
		
		Runnable put = ()-> {
			try {
				while (true)
					bq.put(UUID.randomUUID().toString());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		};
		
		Runnable take = ()-> {
			String uuid = null;
			try {
				while (true) {
					Thread.sleep(500);
					uuid = bq.take();
					System.out.println(uuid);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		};
		
		Thread t1 = new Thread(put, "put-thread");
		Thread t2 = new Thread(take, "take-thread");
		
		t1.start();
		t2.start();
		
		
		System.out.println(bq);
	}
	
	public void test2() throws InterruptedException {
		long t1 = System.nanoTime();
		Thread.sleep(5);
		long t2 = System.nanoTime();
		System.out.println(t1);
		System.out.println(t2);
		System.out.println(t2 - t1);
		System.out.println(((t2 -t1) / 1000) / 1000);
	}
	
	
	public void test3() {
		Object o = new Object();
		System.out.println(o.hashCode());
		System.out.println(System.identityHashCode(o));
		
		System.out.println(Runtime.getRuntime().availableProcessors());
	}
}
